const { contextBridge, ipcRenderer } = require('electron')

// 暴露安全的API到渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  // 下载相关
  startDownload: (config) => ipcRenderer.invoke('start-download', config),
  pauseDownload: (taskId) => ipcRenderer.invoke('pause-download', taskId),
  resumeDownload: (taskId) => ipcRenderer.invoke('resume-download', taskId),
  getDownloadStatus: (taskId) => ipcRenderer.invoke('get-download-status', taskId),
  
  // 任务管理
  getTaskList: () => ipcRenderer.invoke('get-task-list'),
  deleteTask: (taskId) => ipcRenderer.invoke('delete-task', taskId),
  retryTask: (taskId) => ipcRenderer.invoke('retry-task', taskId),
  syncTaskStatus: (taskId, status) => ipcRenderer.invoke('sync-task-status', taskId, status),
  
  // 格式转换
  convertFormat: (inputFile, outputFormat, options) => 
    ipcRenderer.invoke('convert-format', inputFile, outputFormat, options),
  
  // 调试功能
  debugTask: (taskId) => ipcRenderer.invoke('debug-task', taskId),
  debugDelete: (taskId) => ipcRenderer.invoke('debug-delete', taskId),
  getFfmpegInfo: () => ipcRenderer.invoke('get-ffmpeg-info'),
  checkDownloaderStatus: () => ipcRenderer.invoke('check-downloader-status'),
  
  // 文件操作
  selectDirectory: () => ipcRenderer.invoke('select-directory'),
  openFile: (filePath) => ipcRenderer.invoke('open-file', filePath),
  openFileLocation: (filePath) => ipcRenderer.invoke('open-file-location', filePath),
  
  // 事件监听
  onDownloadProgress: (callback) => {
    ipcRenderer.on('download-progress', (event, data) => callback(event, data))
  },
  onDownloadCompleted: (callback) => {
    ipcRenderer.on('download-completed', (event, data) => callback(event, data))
  },
  onDownloadError: (callback) => {
    ipcRenderer.on('download-error', (event, data) => callback(event, data))
  },
  
  // 移除事件监听
  removeAllListeners: (channel) => {
    ipcRenderer.removeAllListeners(channel)
  }
}) 